package org.uned.agonzalo16.bitacora.service.comment;

import java.util.LinkedList;
import java.util.List;

import org.uned.agonzalo16.bitacora.domain.Comment;

public class CommentTree {

	private final List<CommentTree> children = new LinkedList<CommentTree>();
	private final Comment data;

	public CommentTree(Comment data) {
		this.data = data;
	}

	public Comment getComment() {
		return data;
	}

	public void addComment(Comment comment) {
		children.add(new CommentTree(comment));
	}

	public List<CommentTree> getChildren() {
		return children;
	}

	public CommentTree contains(Comment comment) {
		if (data.getId().equals(comment.getId())) {
			return this;
		}

		for (CommentTree child : children) {
			CommentTree result = child.contains(comment);
			if (result != null) {
				return result;
			}
		}

		return null;
	}
}
